/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.exception;

import java.util.Collection;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.spring.initializer.AbstractI18nTest;

/**
 * Generic tests for exceptions.
 */
public abstract class AbstractExceptionTester
    extends AbstractI18nTest
{

  /**
   * This method returns the <code>ExceptionDescriptor</code> of the
   * exceptions to test.
   * 
   * @return the <code>ExceptionDescriptor</code> of the exceptions to test
   */
  public abstract Collection<ExceptionDescriptor<?>> getDescriptors();



  /**
   * Test each exception for creation, throwability and informations.
   */
  @Test
  public void testException()
  {
    for (final ExceptionDescriptor<?> ed : getDescriptors()) {
      testException(ed);
    }
  }



  /**
   * Test the exception describe by <code>exceptionDescriptor</code> for
   * creation, throwability and informations.
   * 
   * @param exceptionDescriptor
   *          the descriptor of the exception to test
   */
  public void testException(final ExceptionDescriptor<?> exceptionDescriptor)
  {
    final String exempleMessage = "message";
    final String exempleKey = "key";
    final Object exempleParameter = new Object();
    final Throwable throwable = exceptionDescriptor.getCause();
    BundledException exception;

    exception = exceptionDescriptor.getException();
    if (exception != null) {
      assertEquals(exception.getMessage(), exceptionDescriptor.getDefaultMessage());
      assertEquals(exception.getI18nMessage().getKey(), exceptionDescriptor.getDefaultKey());
      assertEquals(exception.getI18nMessage().getParameters().get(0), exceptionDescriptor.getDefaultMessage());
      try {
        throw exception;
      }
      catch (final BundledException catchedException) {
        assertEquals(exception, catchedException);
      }
    }

    exception = exceptionDescriptor.getException(exempleMessage, exempleKey, exempleParameter);
    if (exception != null) {
      assertEquals(exception.getMessage(), exempleMessage);
      assertEquals(exception.getI18nMessage().getKey(), exempleKey);
      assertEquals(exception.getI18nMessage().getParameters().get(0), exempleMessage);
      assertEquals(exception.getI18nMessage().getParameters().get(1), exempleParameter);

      try {
        throw exception;
      }
      catch (final BundledException catchedException) {
        assertEquals(exception, catchedException);
      }
    }

    exception = exceptionDescriptor.getException(exempleKey);
    if (exception != null) {
      assertEquals(exception.getI18nMessage().getKey(), exempleKey);

      try {
        throw exception;
      }
      catch (final BundledException catchedException) {
        assertEquals(exception, catchedException);
      }
    }

    exception = exceptionDescriptor.getException(throwable, exempleKey, exempleParameter);
    if (exception != null) {
      assertTrue(exception.getMessage().endsWith(throwable.getMessage()));
      assertEquals(exception.getI18nMessage().getKey(), exempleKey);
      assertTrue(exception.getI18nMessage().getParameters().get(0).toString().endsWith(throwable.getMessage()));
      assertEquals(exception.getI18nMessage().getParameters().get(1), exempleParameter);
      assertEquals(throwable, exception.getCause());

      try {
        throw exception;
      }
      catch (final BundledException catchedException) {
        assertEquals(exception, catchedException);
      }
    }

    exception = exceptionDescriptor.getException(throwable);
    if (exception != null) {
      assertTrue(exception.getMessage().endsWith(throwable.getMessage()));
      assertTrue(exception.getI18nMessage().getParameters().get(0).toString().endsWith(throwable.getMessage()));
      assertEquals(throwable, exception.getCause());

      try {
        throw exception;
      }
      catch (final BundledException catchedException) {
        assertEquals(exception, catchedException);
      }
    }

  }



  @Override
  public Object[][] getKeys()
      throws Exception
  {
    final Object[][] res = new Object[getDescriptors().size()][2];
    int i = 0;
    for (final ExceptionDescriptor<?> ed : getDescriptors()) {
      res[i][0] = ed.getExceptionClass();
      res[i][1] = ed.getDefaultKey();
      i++;
    }
    return res;
  }

}
